<?php
/**
 *
 * @author: xaboy<365615158@qq.com>
 * @day: 2018/01/15
 */

namespace app\pc\controller;


use app\wap\model\user\SmsCode;
use app\pc\model\user\PhoneUser;
use app\pc\model\user\User;
use basic\WapBasic;
use service\SystemConfigService;
use service\UtilService;
use service\JsonService;
use think\Cookie;
use think\Request;
use think\Session;
use think\Url;
use think\Db;
class Login extends WapBasic
{
    public function index()
    {
   
        $login_token = md5(rand(16,1).time());
        $mobile_url= 'http://'.$_SERVER['HTTP_HOST'].Url("/wap/scanlogin/index",array("login_token"=>$login_token));
        $this->assign('mobile_url',$mobile_url);
        $this->assign('login_token',$login_token);
        return $this->fetch();
    }
    public function actionlogin($act)
    {
        if($act=="checklogin")
		{
			$token = trim($this->request->param('login_token'));
			$record = Db::name("user_logintoken")->where('token',$token)->find();
			if(!$record['userid'] || !$record['status']){
                return JsonService::fail(array( "message" => "还没有扫码"));
            }
            $userInfo = User::where('uid', $record["userid"])->find();
            $this->_logout();
            Session::set('loginUid', $userInfo['uid'], 'wap');
            $userInfo['last_time'] = time();
            $userInfo['last_ip'] = $this->request->ip();
            $userInfo->save();
            Cookie::set('is_login', 1);
			//$tUser->qrcodelogin($record['userid']);
			return JsonService::successful( array("message" => "登录成功"));
		}
    }
    /**
     * 短信登陆
     * @param Request $request
     */
    public function phone_check(Request $request)
    {
        list($phone, $code) = UtilService::postMore([
            ['phone', ''],
            ['code', ''],
        ], $request, true);
        if (!$phone || !$code) return $this->failed('请输入登录账号');
        if (!$code) return $this->failed('请输入验证码');
        if (!SmsCode::CheckCode($phone, $code) && !in_array($phone, \think\Config::get('white_phone'))) return JsonService::fail('验证码验证失败');
        SmsCode::setCodeInvalid($phone, $code);
        if (($info = PhoneUser::UserLogIn($phone, $request)) !== false)
            return JsonService::successful('登录成功', $info);
        else
            return JsonService::fail(PhoneUser::getErrorInfo('登录失败'));
    }

    public function user_reg(Request $request)
    {
        list($phone, $pwd, $code) = UtilService::postMore([
            ['phone', ''],
            ['pwd', ''],
            ['code', ''],
        ], $request, true);
        if (!$phone || !$code) return $this->failed('请输入登录账号');
        if (!$code) return $this->failed('请输入验证码');
        if (!$pwd) return $this->failed('请输入登录密码');
        if (!SmsCode::CheckCode($phone, $code)) return JsonService::fail('验证码验证失败');
        SmsCode::setCodeInvalid($phone, $code);
        
        if (($info = PhoneUser::UserLogIn($phone,$pwd, $request)) !== false)
        return JsonService::successful('注册成功', $info);
        else
        return JsonService::fail(PhoneUser::getErrorInfo('注册失败'));

    }
    public function check(Request $request)
    {
        list($account, $pwd, $ref) = UtilService::postMore(['account', 'pwd', 'ref'], $request, true);
        if (!$account || !$pwd) return $this->failed('请输入登录账号');
        if (!$pwd) return $this->failed('请输入登录密码');
        if (!User::be(['account' => $account])) return $this->failed('登陆账号不存在!');
        $userInfo = User::where('account', $account)->find();
        Session::delete('login_error_info', 'wap');
        $errorInfo = Session::get('login_error_info', 'wap') ?: ['num' => 0];
        $now = time();
       
        if ($errorInfo['num'] > 5 && $errorInfo['time'] < ($now - 900))
            return $this->failed('错误次数过多,请稍候再试!');

        if ($userInfo['pwd'] != md5($pwd)) {
            Session::set('login_error_info', ['num' => $errorInfo['num'] + 1, 'time' => $now], 'wap');
            return $this->failed('账号或密码输入错误!');
        }
       
        if (!$userInfo['status']) return $this->failed('账号已被锁定,无法登陆!');
        $this->_logout();
        Session::set('loginUid', $userInfo['uid'], 'wap');
        $userInfo['last_time'] = time();
        $userInfo['last_ip'] = $request->ip();
        $userInfo->save();
        Cookie::set('is_login', 1);
        return JsonService::successful('登录成功', $userInfo);
    }
    public function xieyi(){
        return $this->fetch();
    }
    public function repass(){
        return $this->fetch();
    }
    public function logout()
    {
        $this->_logout();
        $this->successful('退出登陆成功', Url::build('Index/index'));
    }

    private function _logout()
    {
        Session::clear('wap');
        Session::delete(['loginUid', 'loginOpenid']);
        Cookie::delete('is_login');
    }

}
